package com.sdlg.controller;

import com.sdlg.pojo.BookItem;
import com.sdlg.service.BookItemService;
import com.sdlg.util.PageResult;
import com.sdlg.util.QueryPageBean;
import com.sdlg.util.Result;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.UUID;

@RestController
@RequestMapping("/bookitem")
public class BookItemController {

    @Value("${setmealpic.path}")
    private String setmealPath;

    @Resource
    private BookItemService bookitemService;

    //分页展示检查指标数据
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult=bookitemService.findPage(queryPageBean);
        return pageResult;
    }

    //添加指标数据
    @RequestMapping("/saveItem")
    public Result saveItem(@RequestBody BookItem bookItem){
        Result result=bookitemService.saveItem(bookItem);
        return result;
    }

    //根据id删除数据
    @RequestMapping("/deleteById")
    public Result deleteById(Integer id,String img){
        Result result=bookitemService.deleteById(id,img);
        return result;
    }
    @RequestMapping("/getAllItemInfo")
    public Result getAllItemInfo(){
        Result result=bookitemService.getAllItemInfo();
        return result;
    }

    @RequestMapping("/uploadFile")
    public Result uploadFile(MultipartFile imgFile){
        //存放图片的位置
        String path=setmealPath;
        //获取图片的原名称 2.jpg
        String strName=imgFile.getOriginalFilename();
        int lastIndexOf=strName.lastIndexOf('.');
        String str1=strName.substring(lastIndexOf);//.jpg
        String uuidStr= UUID.randomUUID().toString();//342dir32432jewr
        String newFileName=uuidStr+str1;//342dir32432jewr.jpg
        System.out.println("http://localhost:8088/setmealpic/"+newFileName);
        File file=new File(path+newFileName);
        try{
            imgFile.transferTo(file);
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return new Result(true,null,newFileName);
    }
}
